package com.sc.nft.entity.vo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.exception.GlobalRunTimeException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-10-15
 */
@Data
public class UserCollectionConsensusExcelParsingVO {

    @ApiModelProperty(value = "解析结果")
    private List<UserCollectionConsensusExcelParsingListVO> excelList;

    /**
     * 前置验证,判定是否有手机号为空的存在
     * @param voList
     * @return
     */
    public  void  prepositionVerify(List<UserCollectionConsensusExcelParsingListVO> voList) {
        List<UserCollectionConsensusExcelParsingListVO> collect = voList.stream().filter(UserCollectionConsensusExcelParsingListVO -> StrUtil.isBlank(UserCollectionConsensusExcelParsingListVO.getUserTel())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            throw new GlobalRunTimeException("手机号不能为空");
        }
    }

    public void compare(List<UserCollectionConsensusExcelParsingListVO> voList,List<UserCollectionConsensusExcelParsingListVO>userInfoList){
        // voList和userInfoList的userTel差集
        List<UserCollectionConsensusExcelParsingListVO> collect = voList.stream().filter(batchEnterIntegrationShowListVO -> !userInfoList.stream().map(UserCollectionConsensusExcelParsingListVO::getUserTel).collect(Collectors.toList()).contains(batchEnterIntegrationShowListVO.getUserTel())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            throw new GlobalRunTimeException(StrUtil.format("手机号重复或不存在,验证后请重新上传!手机号:{}",collect.stream().map(UserCollectionConsensusExcelParsingListVO::getUserTel).collect(Collectors.joining(","))));
        }
//        Map<String, UserCollectionConsensusExcelParsingListVO> voMap = voList.stream().collect(Collectors.toMap(UserCollectionConsensusExcelParsingListVO::getUserTel, e -> e));
        Map<String, UserCollectionConsensusExcelParsingListVO> userInfoMap = new HashMap<>();
        if (!CollectionUtil.isEmpty(userInfoList)) {
            userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserCollectionConsensusExcelParsingListVO::getUserTel, e -> e));
        }
        for (UserCollectionConsensusExcelParsingListVO userCollectionConsensusExcelParsingListVO : voList) {
            if (!userInfoMap.containsKey(userCollectionConsensusExcelParsingListVO.getUserTel())) {
                throw new GlobalRunTimeException(StrUtil.format("手机号:{}不存在,请检查上传数据",userCollectionConsensusExcelParsingListVO.getUserTel()));
            }
            UserCollectionConsensusExcelParsingListVO consensusExcelParsingListVO = userInfoMap.get(userCollectionConsensusExcelParsingListVO.getUserTel());
            //判断共识度是否是正整数，非正整数报错
            if (userCollectionConsensusExcelParsingListVO.getConsensusNum().compareTo(BigDecimal.ZERO) <= 0) {
                throw new GlobalRunTimeException(StrUtil.format("共识度数量:{}必须是正整数",userCollectionConsensusExcelParsingListVO.getConsensusNum()));
            }
            consensusExcelParsingListVO.setConsensusNum(userCollectionConsensusExcelParsingListVO.getConsensusNum());
        }

    }

}
